<?php

namespace app\mobile\controller;
use app\common\model\CardCzLog;
use app\common\model\CardGivecardLog;
use app\common\model\CardGivePutCard;
use app\common\model\CardMember;
use app\common\model\CardPaycardlog;
use app\common\model\CardUserGet;
use app\common\model\Merchant;
use app\common\model\Momerchant;
use app\common\model\WxOpen;
use think\Controller;
use think\Request;
use think\Session;
use Wechat\Wxobjopen;

class Testget extends Mobilebase
{

    //充值发送礼品领卡
    /*
     * $card_id
     */
    public function getcards()
    {
        $arr = request()->param();
        $this->_init();
        $CardCzLog = new CardCzLog();
        $Merchant=new Merchant();
        $top_mid = $Merchant->where('id', $arr['mid'])->value('top_mid');
        $sinfo = $Merchant->where('id',$arr['mid'])->value('wx_open_id');
        $WxOpen=new WxOpen();
        $sappid=$WxOpen->where('id',$sinfo)->value('authorizer_appid');
        $logopenid = $CardCzLog->where(['id'=>$arr['rec_id']])->value('openid');
        $openid = Session::get($sappid.'_openid');

        //判断领卡人是否正确]
        if ($logopenid == $openid) {
            $map['openid'] = Session::get($sappid.'_openid');
            $midarr = $Merchant->where('top_mid', $top_mid)->field('id')->select();
            foreach ($midarr as $key => $value) {
                $smid[] = $value['id'];
            }
            $mid = implode(",", $smid);
            $map['mid'] = ['in', $mid];
            $map['have'] = 1;
            $map['card_type'] = 'MEMBER_CARD';
            $CardUserGet = new CardUserGet();
            $user = $CardUserGet->where($map)->find();
            if ($user) {
                $CardGivecardLog = new CardGivecardLog();
                $cardlist = $CardGivecardLog->where(['mid' => $arr['mid'], 'give_rec_id' => $arr['rec_id']])->select();

                $this->assign('cardlist', $cardlist);
                return $this->fetch();
            }
        }
        else{
//            var_dump('您没有可领取卡券');
            return $this->fetch('off');
        }
    }

    //充值发送礼品领卡
    /*
     * $card_id
     */
    public function getfirecards()
    {
        $arr = request()->param();
        $this->_init();
        $CardGivePutCard = new CardGivePutCard();
        $Merchant=new Merchant();
        $Momerchant = new Momerchant();
        $CardMermber = new CardMember();
        $top_mid = $CardMermber->where('card_id', $arr['card_id'])->value('top_mid');
        $sinfo = $Momerchant->where('id',$top_mid)->value('wx_open_id');
        $WxOpen=new WxOpen();
        $sappid=$WxOpen->where('id',$sinfo)->value('authorizer_appid');
        $logopenid = $CardGivePutCard->where(['id'=>$arr['rec_id'],'give_type'=>'fire_card'])->value('openid');
        $openid = Session::get($sappid.'_openid');

        //判断领卡人是否正确]
        if ($logopenid == $openid) {
            $map['openid'] = Session::get($sappid.'_openid');
            $midarr = $Merchant->where('top_mid', $top_mid)->field('id')->select();
            foreach ($midarr as $key => $value) {
                $smid[] = $value['id'];
            }
            $mid = implode(",", $smid);
            $map['mid'] = ['in', $mid];
            $map['have'] = 1;
            $map['card_type'] = 'MEMBER_CARD';
            $CardUserGet = new CardUserGet();
            $user = $CardUserGet->where($map)->find();
            if ($user) {
                $CardGivecardLog = new CardGivecardLog();
                $cardlist = $CardGivecardLog->where(['top_mid' => $top_mid, 'give_rec_id' => $arr['rec_id'],'put_type'=>'fire_card'])->select();
                $this->assign('cardlist', $cardlist);
                return $this->fetch();
            }
        }
        else{
            var_dump('您没有可领取卡券');
//            return $this->fetch('off');
        }
    }

    //付费购券列表
    public function getpaycards()
    {
        $arr = request()->param();
        $this->_init();
        $CardPaycardlog = new CardPaycardlog();
        $Merchant=new Merchant();
        $Momerchant = new Momerchant();
        $CardMermber = new CardMember();
        $top_mid = $CardMermber->where('card_id', $arr['card_id'])->value('top_mid');
        $sinfo = $Momerchant->where('id',$top_mid)->value('wx_open_id');
        $WxOpen=new WxOpen();
        $sappid=$WxOpen->where('id',$sinfo)->value('authorizer_appid');
        $logopenid = $CardPaycardlog->where(['id'=>$arr['rec_id']])->value('openid');
        $openid = Session::get($sappid.'_openid');

        //判断领卡人是否正确]
        if ($logopenid == $openid) {
            $map['openid'] = Session::get($sappid.'_openid');
            $midarr = $Merchant->where('top_mid', $top_mid)->field('id')->select();
            foreach ($midarr as $key => $value) {
                $smid[] = $value['id'];
            }
            $mid = implode(",", $smid);
            $map['mid'] = ['in', $mid];
            $map['have'] = 1;
            $map['card_type'] = 'MEMBER_CARD';
            $CardUserGet = new CardUserGet();
            $user = $CardUserGet->where($map)->find();
            if ($user) {
                $CardGivecardLog = new CardGivecardLog();
                $cardlist = $CardGivecardLog->where(['top_mid' => $top_mid, 'give_rec_id' => $arr['rec_id'],'put_type'=>'paycard'])->select();
                $this->assign('cardlist', $cardlist);
                return $this->fetch();
            }
        }
        else{
            var_dump('您没有可领取卡券');
//            return $this->fetch('off');
        }
    }

    //待领取的卡券
    public function getcardslist()
    {
        $arr = request()->param();
        $this->_init();
        $appid = $this->getappid($arr['card_id']);
        $map['openid'] = Session::get($appid.'_openid');
        $map['card_id'] = $arr['card_id'];
        $map['have'] = 1;
        $map['card_type'] = 'MEMBER_CARD';
        $CardUserGet = new CardUserGet();
        $user = $CardUserGet->where($map)->field('card_id,code,top_mid,openid,nickname,mid')->find();
        if ($user) {
            $user['get_time'] = $user['create_time'];
            $this->checkfirecards($user);
            $CardGivecardLog = new CardGivecardLog();
//                $mab['card_id'] = $arr['card_id'];
//                $mab['code'] = $user['code'];

            $cardlist = $CardGivecardLog->where(['card_id' => $arr['card_id'], 'code' => $user['code']])->select();
//                foreach ($cardlist as $k=>$v){
//                    if ($v['put_sume'] > $v['get_sume']){
//                        $cardlist[$k] = $v;
//                    }
//                }

//                if ($user['top_mid'] == 1){
//                    var_dump($CardGivecardLog->getLastSql());
//                    var_dump($cardlist);
//                }
            $this->assign('card',$arr);
            $this->assign('user',$map);
            $this->assign('cardlist', $cardlist);
            return $this->fetch();

        }else{
//            var_dump('您没有可领取卡券');
            return $this->fetch('off');
        }
    }

    //gettest
    public function getlist(){
        $arr = request()->param();
        $this->_init();
        $CardCzLog = new CardCzLog();
        $Merchant=new Merchant();
        $top_mid = $Merchant->where('id', $arr['mid'])->value('top_mid');
        $sinfo = $Merchant->where('id',$arr['mid'])->value('wx_open_id');
        $WxOpen=new WxOpen();
        $sappid=$WxOpen->where('id',$sinfo)->value('authorizer_appid');
        $logopenid = $CardCzLog->where(['id'=>$arr['rec_id']])->value('openid');
        $openid = Session::get($sappid.'_openid');
        //判断领卡人是否正确]
        if ($logopenid == $openid) {
            $map['openid'] = Session::get($sappid.'_openid');
            $midarr = $Merchant->where('top_mid', $top_mid)->field('id')->select();
            foreach ($midarr as $key => $value) {
                $smid[] = $value['id'];
            }
            $mid = implode(",", $smid);
            $map['mid'] = ['in', $mid];
            $map['have'] = 1;
            $map['card_type'] = 'MEMBER_CARD';
            $CardUserGet = new CardUserGet();
            $user = $CardUserGet->where($map)->find();
            if ($user) {
                $CardGivecardLog = new CardGivecardLog();
                $cardlist = $CardGivecardLog->where(['mid' => $arr['mid'], 'give_rec_id' => $arr['rec_id']])->select();
                 foreach ($cardlist as $k => $v) {
                     if ($v['put_sume'] > $v['get_sume']) {
                         for ($x = 0; $x < $v['put_sume']; $x++) {
//                        var_dump(json_encode($v));
                             $options = $this->options($v['zs_card_id']);
                             $arrs['card_id'] = $v['zs_card_id'];
                             $arrs['openid'] = $openid;
                             $arrs['outer_str'] = 'giveRecId_' . $v['id'];
                             $card[] = $this->putlist($arrs);
                         }
                     }
                }
                $this->assign('cardlist',$cardlist);
                if (isset($options)) {
                    $weObj = new Wxobjopen($options);
                    $url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                    $jssdk = $weObj->getJsSign($url);
                    $this->assign('card', json_encode($card));
                    $this->assign('jssdk', $jssdk);
                    return $this->fetch();
                }else{
                    return $this->fetch('cardget/getoff');
                }
            }
        }

    }

    //批量投放
    /*
     * $arr['openid'] / $arr['card_id']
     */
    private function putlist($arr){
        $options = $this->options($arr['card_id']);
        $weObj = new Wxobjopen($options);
        $noncestr = $weObj->generateNonceStr();
        $outer_str = $arr['outer_str'];
        $res['cardId'] = $arr['card_id'];
        $res['cardExt'] = $weObj->wxAddCard($arr['openid'], $arr['card_id'], $noncestr,$outer_str);
        return $res;
    }

    //Ajax打开领取
    /*
     * $arr['card_id']
     */
    public function getaddcard(){
        if (Request()->isAjax()){
            $arr = request()->post();
            $CardGivecardLog = new CardGivecardLog();
            $log = $CardGivecardLog->where(['id'=>$arr['givelogid']])->find();
            if ($log['get_sume'] < $log['put_sume']) {
//            return json($arr);
                $url = $arr['url'];

//            $url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                $options = $this->options($arr['card_id']);
                //发放
                $arrs['card_id'] = $arr['card_id'];
                $arrs['openid'] = Session::get($options['authorizer_appid'].'_openid');
                $arrs['outer_str'] = 'giveRecId_' . $arr['givelogid'];
                $weObj = new Wxobjopen($options);
//            $tick = $weObj->getCardJsTicket();
//            return json($tick);
                $jssdk = $weObj->getJsSign($url);

//            return json($jssdk);
                //输出内容
                $card['card'] = $this->put($arrs);
                $card['jssdk']['debug'] = false;
                $card['jssdk']['api_ticket'] = $jssdk['ticket'];
                $card['jssdk']['url'] = $jssdk['url'];
                $card['jssdk']['appId'] = $jssdk['appId'];
                $card['jssdk']['timestamp'] = $jssdk['timestamp'];
                $card['jssdk']['nonceStr'] = $jssdk['nonceStr'];
                $card['jssdk']['signature'] = $jssdk['signature'];
                $card['jssdk']['jsApiList'] = ['addCard'];
                return json($card);
            }else{
                $card = ['code'=>9001,'msg'=>'已经领完了'];
                return json($card);
            }
        }
    }

    public function getaddcards(){
        if (Request()->isAjax()){
            $arr = request()->post();

            $url = $arr['url'];
            $options = $this->options($arr['card_id']);
            //发放
            $arrs['card_id'] = $arr['card_id'];

            $arrs['openid'] = Session::get($options['authorizer_appid'].'_openid');

            $arrs['outer_str'] = 'Cent';
            $weObj = new Wxobjopen($options);

            $jssdk = $weObj->getJsSign($url);

            //输出内容
            $card['card'] = $this->put($arrs);
            $card['jssdk']['debug'] = false;
            $card['jssdk']['api_ticket'] = $jssdk['ticket'];
            $card['jssdk']['url'] = $jssdk['url'];
            $card['jssdk']['appId'] = $jssdk['appId'];
            $card['jssdk']['timestamp'] = $jssdk['timestamp'];
            $card['jssdk']['nonceStr'] = $jssdk['nonceStr'];
            $card['jssdk']['signature'] = $jssdk['signature'];
            $card['jssdk']['jsApiList'] = ['addCard'];
            return json($card);
        }else{
            $card = ['code'=>9001,'msg'=>'已经领完了'];
            return json($card);
        }

    }

    //Ajax打开领取
    /*
     * $arr['card_id']
     */
    public function getaddcardshop(){
        if (Request()->isAjax()){
            $arr = request()->post();
            $url = $arr['url'];
            $options = $this->options($arr['card_id']);
            //发放
            $arrs['card_id'] = $arr['card_id'];
            $arrs['openid'] = Session::get($options['authorizer_appid'].'_openid');
            $arrs['outer_str'] = 'shopExch_' . $arr['orderid'];
            $weObj = new Wxobjopen($options);
            $jssdk = $weObj->getJsSign($url);
            //输出内容
            $card['card'] = $this->put($arrs);
            $card['jssdk']['debug'] = true;
            $card['jssdk']['api_ticket'] = $jssdk['ticket'];
            $card['jssdk']['url'] = $jssdk['url'];
            $card['jssdk']['appId'] = $jssdk['appId'];
            $card['jssdk']['timestamp'] = $jssdk['timestamp'];
            $card['jssdk']['nonceStr'] = $jssdk['nonceStr'];
            $card['jssdk']['signature'] = $jssdk['signature'];
            $card['jssdk']['jsApiList'] = ['addCard'];
            return json($card);

        }
    }

    //投放卡
    /*
     * $arr['openid'] / $arr['card_id']
     */
    private function put($arr)
    {
        $options = $this->options($arr['card_id']);
        $weObj = new Wxobjopen($options);
        $noncestr = $weObj->generateNonceStr();
        $outer_str = $arr['outer_str'];
        $res['card_id'] = $arr['card_id'];
        $res['ext'] = $weObj->wxAddCard('', $arr['card_id'], $noncestr,$outer_str);
        return $res;
    }

    //分享jssdk
    /*
     * $card_id
     * 
     */
    private function getsharejssdk($arr){
        $url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $options = $this->options($arr);
        //分享
        $weObj = new Wxobjopen($options);
        $jssdk = $weObj->getJsSign($url);
        $wxJsSdk = [
            'debug' => false,
            'appId' => $jssdk['appId'],
            'timestamp' =>$jssdk['timestamp'],
            'nonceStr' => $jssdk['nonceStr'],
            'signature' => $jssdk['signature'],
            'jsApiList' => [
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'onMenuShareQQ',
                'onMenuShareQZone',
                'closeWindow',
                'hideAllNonBaseMenuItem',
            ]
        ];
        return $wxJsSdk;
    }



}
